package com.guan.core.util;



import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

/**
 * 工具类-操作xml的api
 * Created by Administrator on 2018/1/9 0009.
 */
public class XMLUtil {
    /**
     * 通过输入流拿到一个document对象
     * @param in
     * @return
     */
    public static Document getDocFromFile(InputStream in){
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(in);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return doc;
    }

    /**
     * 获取xml最外层根节点
     * @param document
     * @return
     */
    public static Element getRootElement(Document document){
        if(null == document){
            return null;
        }
        Element root = document.getRootElement();
        return root;
    }

    /**
     * 通过父节点，拿到全部子节点
     * @param parent
     * @return
     */
    public static List<Element> getChildElements(Element parent){
        if(null == parent){
            return null;
        }
        List<Element> list = parent.elements();
        return list;
    }
    public static void log(String log){
        System.out.println(log);
    }
}
